package com.tiantian.quanminxiu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.dialog.Dialog_EditName;
import com.tiantian.quanminxiu.dialog.Dialog_EditSex;
import com.tiantian.quanminxiu.presenter.EditInfoPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.CommonEnum;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.EditInfoView;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/12.
 */
public class Activity_EditInfo extends ActivityBase implements EditInfoView {

    private EditInfoPresent present;

    private View vUserAvatar,vUserName,vUserSign,vUserSex,vRootView;
    private ImageView imgUserAvatar;
    private TextView txtUserName,txtUserSex,txtUserSign;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editinfo);

        Init();
        InitView();

        present = new EditInfoPresent();
        present.attachView(this);

        UserData.Reload.Activity_EditInfo = true;
    }

    @Override
    public void InitView() {
        vRootView =findViewById(R.id.vRootView);
        vUserAvatar =findViewById(R.id.vUserAvatar);
        vUserName =findViewById(R.id.vUserName);
        vUserSign =findViewById(R.id.vUserSign);
        vUserSex =findViewById(R.id.vUserSex);

        txtUserName = (TextView) findViewById(R.id.txtUserName);
        txtUserSex = (TextView) findViewById(R.id.txtUserSex);
        txtUserSign = (TextView) findViewById(R.id.txtUserSign);

        imgUserAvatar = (ImageView) findViewById(R.id.imgUserAvatar);

        RxView.clicks(vUserAvatar).map(View->vUserAvatar).subscribe(OC_Button);
        RxView.clicks(vUserName).map(View->vUserName).subscribe(OC_Button);
        RxView.clicks(vUserSign).map(View->vUserSign).subscribe(OC_Button);
        RxView.clicks(vUserSex).map(View->vUserSex).subscribe(OC_Button);
        RxView.clicks(mHeader.txtHeader_Enter).map(View->mHeader.txtHeader_Enter).subscribe(OC_Button);
    }

    @Override
    public void PageInit() {
        if(!UserData.Reload.Activity_EditInfo){
            return;
        }
        UserData.Reload.Activity_EditInfo = false;

        txtUserSex.setText(UserData.Base.Sex==1?"男":"女");
        txtUserName.setText(UserData.Base.NickName);
        txtUserSign.setText(UserData.Base.Signature);

        imgUserAvatar.post(new Runnable() {
            @Override
            public void run() {
                APPUtils.displayImage(APPUtils.createUserAvatar(UserData.Base.User_ID), imgUserAvatar,thisActivity, imgUserAvatar.getHeight() / 2);
            }
        });
    }

    private Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.txtHeader_Enter:
                present.saveUserInfo(txtUserName.getText().toString(),txtUserSign.getText().toString(),txtUserSex.getText().toString().equalsIgnoreCase("男")?1:2);
                break;
            case R.id.vUserAvatar:
                APPUtils.UploadPhoto_To(thisActivity,"","",-1);
                break;
            case R.id.vUserName:
                Dialog_EditName.show(vRootView, thisContext,
                        this);
                break;
            case R.id.vUserSign:
                APPUtils.Signature_TO(thisActivity, CommonEnum.RequestCode.Activity_Signature);
                break;
            case R.id.vUserSex:
                Dialog_EditSex.show(vRootView, thisContext,
                        this);
                break;
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    public void setUserName(String userName) {
        txtUserName.setText(userName);
    }

    @Override
    public void setUserSex(int sex) {
        txtUserSex.setText(sex==1?"男":"女");
    }

    @Override
    public void setUserPic() {

    }

    @Override
    public void setUserSign(String sign) {
        txtUserSign.setText(sign);
    }

    @Override
    public void saveUserInfo() {
        CommonUtils.showToask(thisContext,"修改成功");
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        present.detachView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CommonEnum.RequestCode.Activity_Signature:
                    String signature = data.getStringExtra("signature");
                    setUserSign(signature);
                    break;
            }
        }
    }
}
